package com.roads.project.controller;

import com.roads.common.annotation.Log;
import com.roads.common.constant.UserConstants;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.common.utils.StringUtils;
import com.roads.project.domain.ProSupervisorBoq;
import com.roads.project.service.IProSupervisorBoqService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * 项目监理清单Controller
 * 
 * @author nhp
 * @date 2022-07-08
 */
@RestController
@RequestMapping("/project/supervisor/boq")
public class ProSupervisorBoqController extends BaseController {

    @Autowired
    private IProSupervisorBoqService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProSupervisorBoq model) {
        startPage();
        List<ProSupervisorBoq> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 查询清单列表
     */
    @GetMapping("/getTreeList")
    public AjaxResult getTreeList(ProSupervisorBoq boq) {
        List<ProSupervisorBoq> list = service.selectTreeList(boq);
        return AjaxResult.success(list);
    }

    /**
     * 查询标段清单列表 排除子项
     */
    @GetMapping("/getTreeListExcludeChild")
    public AjaxResult getTreeListExcludeChild(ProSupervisorBoq model) {
        List<ProSupervisorBoq> list = service.selectTreeList(model);
        model = service.getModelById(model.getId());
        Iterator<ProSupervisorBoq> it = list.iterator();
        while (it.hasNext()) {
            ProSupervisorBoq b = it.next();
            if (b.getId().equals(model.getId()) ||
                    ArrayUtils.contains(StringUtils.split(b.getAncestors(), ","), model.getBoqCode() + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(service.getModelById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目监理清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProSupervisorBoq model) {
        if (UserConstants.NOT_UNIQUE.equals(service.checkBoqCodeUnique(model))) {
            return AjaxResult.error("新增清单'" + model.getBoqCode() + "'失败，清单编号已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 同步系统监理清单
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目监理清单", businessType = BusinessType.INSERT)
    @GetMapping(value = "/sync/{projectId}")
    public AjaxResult sync(@PathVariable("projectId") Integer projectId) {
        ///// 同步之前验证
        return toAjax(service.sync(projectId));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "系统监理清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProSupervisorBoq model) {
        if (UserConstants.NOT_UNIQUE.equals(service.checkBoqCodeUnique(model))) {
            return AjaxResult.error("修改清单'" + model.getBoqCode() + "'失败，清单编号已存在");
        }
        model.setUpdateBy(getUsername());
        service.update(model);
        return success();
    }

    /**
     * 删除
     */
    @Log(title = "系统监理清单", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        // 删除限制
        for (int i = 0; i < ids.length; i++) {
            // 是否叶子节点，如果不是叶子节点不能删除
            ProSupervisorBoq boq = service.getModelById(ids[i]);
            List<ProSupervisorBoq> childrenList = service.selectChildrenList(boq.getProjectId(), boq.getBoqCode());
            if (childrenList.size() > 0) {
                return error("选中的清单包含子清单，不允许删除");
            }
        }
        return toAjax(service.deleteByIds(ids));
    }
}
